package com.hyphenate.easeui.widget.chatrow;

import android.content.Context;
import android.text.Spannable;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.exceptions.HyphenateException;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.ChatCard;
import com.joplus.housekeeper.bean.PlanTotalBean;
import com.joplus.housekeeper.utils.DateUtils;
import com.joplus.housekeeper.utils.StringUtils;

import org.json.JSONObject;


/**
 * 行程总揽
 */
public class EaseChatRowText_Plan_Today extends EaseChatRow {


    private TextView tv_head_l;//温度
    private ImageView tv_weather;//天气图标


    private TextView tv_foot_l;
    private TextView tv_foot_r;
    private TextView tv_head_r;//事件数
    LinearLayout layout_plan;

    static PlanTotalBean planTotalBean;

    int[] ids = {R.drawable.pan_card_bg_1, R.drawable.pan_card_bg_2, R.drawable.pan_card_bg_3, R.drawable.pan_card_bg_4, R.drawable.pan_card_bg_5, R.drawable.pan_card_bg_6, R.drawable.pan_card_bg_7};

    public EaseChatRowText_Plan_Today(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);

    }

    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_message_plan_today : R.layout.ease_row_sent_message, this);
    }

    @Override
    protected void onFindViewById() {


        tv_head_l = (TextView) findViewById(R.id.tv_head_l);
        tv_weather = (ImageView) findViewById(R.id.tv_weather);
        tv_foot_l = (TextView) findViewById(R.id.tv_foot_l);
        tv_foot_r = (TextView) findViewById(R.id.tv_foot_r);
        tv_head_r = (TextView) findViewById(R.id.tv_head_r);
        layout_plan = (LinearLayout) findViewById(R.id.layout_plan);
        layout_plan.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    protected void onUpdateView() {

    }

    @Override
    protected void onSetUpView() {
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
        // 设置内容
//        tv_chatcontent.setText(span, TextView.BufferType.SPANNABLE);
        String msg = txtBody.getMessage();

        try {
            new JSONObject(msg);
            Gson gson = new Gson();
            ChatCard chatCard = gson.fromJson(txtBody.getMessage(), ChatCard.class);


            tv_head_l.setText(chatCard.getPlanTotalBean().getWeatherBean().getTem());
            if (chatCard.getPlanTotalBean() != null && chatCard.getPlanTotalBean().getData().getTravelPlanDTO() != null && chatCard.getPlanTotalBean().getData().getTravelPlanDTO().getTravelDate() != null) {
                String date = chatCard.getPlanTotalBean().getData().getTravelPlanDTO().getTravelDate();
                StringUtils.setText(tv_foot_l, date);

                StringUtils.setText(tv_foot_r, DateUtils.dayFroWeek2(date));


            }

            if (chatCard.getPlanTotalBean() != null && chatCard.getPlanTotalBean().getData().getTravelPlanDTO() != null) {
                StringUtils.setText(tv_head_r, chatCard.getPlanTotalBean().getData().getTravelPlanDTO().getTravelCount() + "个事件");
            }


        } catch (Exception e) {


        }


    }

    @Override
    protected void onBubbleClick() {

    }

    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    // 发送消息
                    //               sendMsgInBackground(message);
                    break;
                case SUCCESS: // 发送成功
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL: // 发送失败
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS: // 发送中
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        } else {
            if (!message.isAcked() && message.getChatType() == EMMessage.ChatType.Chat) {
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void insertCard(final Context context, final EaseChatRowText_RecommandCard.InsertRecommandCard i) {
        //获取总揽卡片的信息

    }
}
